package com.wiki.controller;


import com.alibaba.fastjson.JSONObject;
import com.wiki.entity.User;
import com.wiki.service.UserService;
import com.wiki.utils.CopyUtil;
import com.wiki.utils.SnowFlake;
import com.wiki.utils.commonRequest.user.UserLoginReq;
import com.wiki.utils.commonRequest.user.UserQueryReq;
import com.wiki.utils.commonRequest.user.UserResetPasswordReq;
import com.wiki.utils.commonRequest.user.UserSaveReq;
import com.wiki.utils.commonResponse.CommonResp;
import com.wiki.utils.commonResponse.UserLoginResp;
import com.wiki.utils.commonResponse.UserQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SnowFlake snowFlake;


    @GetMapping("/list")
    public CommonResp list(@Valid UserQueryReq req) {
        CommonResp<PageResp<UserQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<UserQueryResp> name = userService.listAll(req);
        listCommonResp.setContent(name);
        return listCommonResp;
    }

    @GetMapping("/list_by_name")
    public CommonResp list_by_name(@Valid UserQueryReq req) {
        CommonResp<PageResp<UserQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<UserQueryResp> resp = userService.queryLike(req);
        listCommonResp.setContent(resp);
        return listCommonResp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req) {
        CommonResp resp = new CommonResp();
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8)));
        userService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp();
        userService.delete(id);

        return resp;
    }

    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq req) {
        CommonResp resp = new CommonResp<>();
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8)));
        userService.resetPassword(req);
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req) {
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8)));
        UserLoginResp userLoginResp = userService.login(req);


        Long token = snowFlake.nextId();
        LOG.info("生成单点登录token:{}，并放入redis", token.toString());
        userLoginResp.setToken(token.toString());
        redisTemplate.opsForValue().set(token, JSONObject.toJSONString(userLoginResp), 3600 * 24, TimeUnit.SECONDS);

        resp.setContent(userLoginResp);
        return resp;
    }

    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token){
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        LOG.info("从redis中删除token：{}",token);
        resp.setMessage("退出成功");
        return resp;


    }

}
